﻿using System;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Data.OleDb;
using System.Data.SqlClient;
using Negocio;

namespace Persistencia
{
    class BrokerPlayer : Broker
    {
        public BrokerPlayer()
        {
            this.conn = ConnectionDB.getInstance();
        }

        public override void add(Object o)
        {
            Player playerAux = (Player)o;
            this.conn.connect();
                 string sql ="insert into Player(name,firstLastName,secondLastName,Oid,birthDate,identityDocument,club,height,weight,enabled,admissionDate,leg, posicion, photo_) values('" + playerAux.Name + "', '" + playerAux.FirstLastname + "','" + playerAux.SecondLastname + "'," + playerAux.OID + ",'" + playerAux.BirthDate + "','" + playerAux.IdentityDocument + "','" + playerAux.Club + "','" + playerAux.Height + "','" + playerAux.Weight + "'," + playerAux.Enabled + ",'" + playerAux.AdmissionDate + "','" + playerAux.Leg + "','" + playerAux.Position + "', '" + playerAux.Photo + "')";
                 string sql1 ="update OID set OId=" + playerAux.OID;
                 this.conn.execute(sql);
                 this.conn.execute(sql1);
                this.conn.disconnect();
        }

        public override void update(Object o)
        {
            Player playerAux = (Player)o;
            string sql = "update Player set name='"
                + playerAux.Name + "', firstLastname='" + playerAux.FirstLastname
                + "',secondLastname = '" + playerAux.SecondLastname + "',birthDate = '"
                + playerAux.BirthDate + "',phone = '" + playerAux.Phone + "',identityDocument = '"
                + playerAux.IdentityDocument + "',admissionDate = '"
                + playerAux.AdmissionDate + "', weight = '" + playerAux.Weight
                + "' ,height = '" + playerAux.Height + "',email = '" + playerAux.EMail + "', club = '" 
                + playerAux.Club + "',enabled=" + playerAux.Enabled + ",photo_ = '" 
                + playerAux.Photo + "', leg = '" + playerAux.Leg + "', posicion = '" 
                + playerAux.Position + "' where oid = " + playerAux.OID;
            this.conn.connect();
            this.conn.execute(sql);
            this.conn.disconnect();
        }

        public override void delete(Object o)
        {
            Player playerAux = (Player)o;
            string sql = "update Player set enable='F' where oid=" + playerAux.OID;
            this.conn.connect();
            this.conn.execute(sql);
            this.conn.disconnect();
        }

        public override void bringData(Object o)
        {
            Player playerAux = (Player)o;
            this.conn.connect();
            string sql = "Select * from Player where oid =" + playerAux.OID;
            this.conn.bringData(sql);
            while (this.conn.nextRecord())
            {
                playerAux.Name = this.conn.getDataString("name").ToString();
                playerAux.IdentityDocument = this.conn.getDataString("identityDocument").ToString().ToString();
                playerAux.FirstLastname = this.conn.getDataString("firstLastname").ToString();
                playerAux.SecondLastname = this.conn.getDataString("secondLastname").ToString();
                playerAux.Phone = this.conn.getDataString("phone").ToString();
                playerAux.BirthDate = (DateTime)this.conn.getDataString("birthDate");
                playerAux.AdmissionDate = (DateTime)this.conn.getDataString("admissionDate");
                playerAux.Weight = float.Parse(this.conn.getDataString("weight").ToString());
                playerAux.Height = float.Parse(this.conn.getDataString("height").ToString());
                playerAux.EMail = this.conn.getDataString("email").ToString();
                playerAux.Club = this.conn.getDataString("club").ToString();
                playerAux.Photo = this.conn.getDataString("photo_").ToString();
                playerAux.Leg = this.conn.getDataString("leg").ToString();
                playerAux.Position = this.conn.getDataString("posicion").ToString();
                playerAux.Enabled = (bool)this.conn.getDataString("enabled");
                playerAux.OID = int.Parse(this.conn.getDataString("Oid").ToString());
            }
            this.conn.disconnect();

        }
        public override ObservableCollection<Object> bringList(Object o)
        {
            string sql = "Select * from Player order by firstLastName, secondLastName, name asc";
            ObservableCollection<Object> lista = new ObservableCollection<Object>();
            this.conn.connect();
            this.conn.bringList(sql);
            while (this.conn.nextRecord())
            {
                Player player = new Player();
                player.Name = this.conn.getDataString("name").ToString();
                player.IdentityDocument = this.conn.getDataString("identityDocument").ToString();
                player.FirstLastname = this.conn.getDataString("firstLastname").ToString();
                player.SecondLastname = this.conn.getDataString("secondLastname").ToString();
                player.Phone = this.conn.getDataString("phone").ToString();
                player.BirthDate = (DateTime)this.conn.getDataString("birthDate");
                player.AdmissionDate = (DateTime)this.conn.getDataString("admissionDate");
                player.Weight = float.Parse(this.conn.getDataString("weight").ToString());
                player.Height = float.Parse(this.conn.getDataString("height").ToString());
                player.EMail = this.conn.getDataString("email").ToString();
                player.Club = this.conn.getDataString("club").ToString();
                player.Photo = this.conn.getDataString("photo_").ToString();
                player.Leg = this.conn.getDataString("leg").ToString();
                player.Position = this.conn.getDataString("posicion").ToString();
                player.Enabled = (bool)this.conn.getDataString("enabled");
                player.OID = int.Parse(this.conn.getDataString("OId").ToString());
                lista.Add(player);
            }
            this.conn.disconnect();
            return lista;
        }

    }
}
